<?php
namespace ZL;
use Think\Controller as BaseController;

class AutoPay extends BaseController{

    //默认设置array数组
    protected $configArr = array(
        '2' => array(
            'limitMoney' => 10000,
            'level' => array(
                '1' => 0,
                '2' => 0.2,
                '3' => 0.1,
                '4' => 0.08,
                '5' => 0.03,
                True => 0.03,
            )
        ),
        '3' => array(
            'limitMoney' => 15000,
            'level' => array(
                '1' => 0,
                '2' => 0.2,
                '3' => 0.1,
                '4' => 0.08,
                '5' => 0.03,
                True => 0.03,
            )
        ),
        '4' => array(
            'limitMoney' => 20000,
            'level' => array(
                '1' => 0,
                '2' => 0.2,
                '3' => 0.1,
                '4' => 0.08,
                '5' => 0.03,
                True => 0.03,
            )
        ),
    );
    //利息设置
    protected $interest = 0.01;

    //用户获得的利息金额
    protected $userInt = array();

    //用户关系
    protected $userRelation = array();

    //订单查询状态
    protected $status;

    //订单修改状态
    protected $doneStatus;

/**
 * 构造函数 可以在构造函数里修改默认配置
 * @param int $status 模型名
 */
    public function __construct($status, $doneStatus=false, $configArr=false) {
        parent::__construct();
        //查询状态设置
        $this->status = $status;
        //修改状态设置
        if($doneStatus)
            $this->doneStatus = $doneStatus;
        //配置数组设置
        if($configArr)
            $this->configArr = $configArr;
        //利息设置
        $interest = M('SystemConfig')->where(array('name'=>'LBW_ZYLXLV_DY'))->find();
        $this->interest = $interest['value'];
    }

/**
 * 查询函数
 * @param int $status 查询订单的状态
 * @param int $doneStatus 完成订单的状态
 */
    public function payInterest() {
        $data = M('UserTuiguang')->select();
        foreach ($data as $k => $v){
            $_data[$v['id']] = $v['parentId'];
        }
        $this->userRelation = $_data;
        $data = M('UserTemppipeiorder')->where(array('jiaoyi_zt'=>$this->status))->select();
        echo '<pre>';
        var_dump($data);
        foreach ($data as $k=>$v){
            $id = $v['lbw_vipid'];
            $user = M('User')->find($id);
            $vip = $user['vip'];
            $this->_reSort($v,$vip);
            var_dump($v);
            echo 'vip';
            echo $vip;
            //可以设置修改状态
            if($this->doneStatus)
                $v['jiaoyi_zt'] = $this->doneStatus;
            M('UserTemppipeiorder')->data($v)->save();
        }
    }

    public function _reSort($data, $vip){
        switch($vip){
            case 0:
                break;
            case 1:
                break;
            case 2:
                $this->getTree($data, $vip, $this->configArr['2']);
                break;
            case 3:
                $this->getTree($data, $vip, $this->configArr['3']);
                break;
            case 4:
                $this->getTree($data, $vip, $this->configArr['4']);
                break;
        }
    }
    //无用过度
    public function getTree($data, $vip, $config)
    {
        echo '配置';
        var_dump($config);
        var_dump($data);
        $this->recursion($data, $data['lbw_vipid'], $vip, $config);
    }
    //递归函数
    public function recursion($data, $id, $vip, $config, $level=1){
        echo 'recursion';
        echo '<br/>';
        echo 'rule';
        var_dump($this->userRelation);
        var_dump($config);
        echo 'id:::::::';
        var_dump($id);
        var_dump($level);
        if($level)
            $this->addInt($data, $id, $config, $level);
        $level++;
        if ($pid = $this->userRelation[$id])
            $this->recursion($data, $pid, $vip, $config, $level);
    }

    public function addInt($data, $id, $config, $level){
        
        echo 'addint';
        echo '<br/>';
        var_dump($data);
        var_dump($id);
        var_dump($config);
        echo '等级————————————————————————————————';
        var_dump($level);
        foreach ($config['level'] as $k=>$v){
            if($k==$level && $v!=0){
                var_dump($k);
                $this->saveData($data, $id, $v, $config['limitMoney']);
            }
        }
    }

     public function saveData($data, $id, $percent, $limit){
        echo '最后的方法 保存！';
        echo '<br/>';
        var_dump($limit);
        var_dump($data);
        var_dump($id);
        var_dump($percent);
        $money = (int)$data['total_forever'];
         $dh   = (int)$data['lbw_order_pipei'];  //产生利息交易订单
        $userid   = $id;  //获得利息id
        $tigong_userid=(int)$data['lbw_vipid']; //提供利息id
        $lixi_time = time(); //当前时间
        $dk_money=(int)$data['total_forever']; //打款金额
        $money = $money*$this->interest*$percent;
        if($this->userInt['$id'] == $limit)
            return true;
        elseif (($this->userInt['$id']+$money)>$limit){
            $money = $limit - $this->userInt['$id'];
        }
        //保存利息表
        $_data['lixi_money'] = $money;
        $_data['dk_sf_jydh'] = $dh;
        $_data['username_sy'] = $userid;
        $_data['dk_lbw_vipid'] = $tigong_userid;
        $_data['lixi_time'] = $lixi_time;
        $_data['dk_money_total'] = $dk_money;
        $ok = M('UserLixi')->add($_data);
        echo '<pre>';
        var_dump($_data);
        echo $ok;
        if($ok)
            $this->userInt[$id] += $money;
    }
}


//    public function saveData($data, $id, $percent, $limit){
//        $money = (int)$data['money_total'];
//        $money = $money*$this->interest*$percent;
//        if($this->userInt['$id'] == $limit)
//            return true;
//        elseif (($this->userInt['$id']+$money)>$limit){
//            $money = $limit - $this->userInt['$id'];
//        }
//        //保存利息表
//        $_data['lixi_money'] = $money;
//        $ok = M('UserLixi')->add($_data);
//        if($ok)
//            $this->userInt[$id] += $money;
//    }
//}